package com.shanghaionstar.carmanage;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.PackageType;
import com.onstar.cn.more.entity.OrderHistoryType;
import com.shanghaionstar.R;
import com.shanghaionstar.carmanage.adapter.OrderRecordsAdapter;
import com.shanghaionstar.carmanage.iview.IOrderRecordView;
import com.shanghaionstar.carmanage.presenter.OrderRecordPresenter;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.weight.CommonNoDatasLayout;
import com.shanghaionstar.weight.PaymeDialog;

import java.util.List;

/**
 * 订单记录列表
 * Created by Yonglang Hong on 2016/7/29.
 */
@ContentView(R.layout.car_manage_activity_order_record)
public class OrderRecordActivity extends SwipeActivity {

    public static final String PACKAGE_TYPE = "packageType";

    @Override
    public BasePresenter getPresenter() {
        return null;
    }
    @FindViewById
    private View progressLayout,more_progress;
    @FindViewById
    private ListView lsv_orders;
    @FindViewById
    private View rl_list,rl_more;
    @FindViewById
    private TextView tv_more;

    @FindViewById
    private CommonNoDatasLayout cndl_noDatas;

    OrderRecordPresenter recordsPresenter;

    OrderRecordsAdapter recordsAdapter;

    PackageType packageType;

    int pageNum = 0,pageSize = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init(){
        progressLayout.setVisibility(View.VISIBLE);
        initReqEntity();
        setRecordsAdapter();
        loadOrderRecords();
        setLoadMoreListener();
    }

    private void initReqEntity(){
        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            boolean isEBPackage = bundle.getBoolean("isEBPackage", false);
            if (isEBPackage) {
                Dialog paymeEBDialog = new PaymeDialog(this, R.style.PaymeEBDialog);
                paymeEBDialog.show();
            }
            packageType = (PackageType) bundle.getSerializable(PACKAGE_TYPE);
        }
    }

    private void setLoadMoreListener(){
        rl_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                more_progress.setVisibility(View.VISIBLE);
                tv_more.setVisibility(View.GONE);
                loadOrderRecords();
            }
        });
    }

    private void loadOrderRecords(){
        pageNum = +1;
        pageSize = recordsAdapter.getCount();
        getRecordPresenter().loadOrderRecords(packageType,pageNum,pageSize);
    }

    private void setRecordsAdapter(){
        if (recordsAdapter == null){
            recordsAdapter = new OrderRecordsAdapter(this,packageType);
        }
        lsv_orders.setAdapter(recordsAdapter);
    }

    public OrderRecordPresenter getRecordPresenter() {
        if (recordsPresenter == null){
            recordsPresenter = new OrderRecordPresenter(getScheduler(),this);
            recordsPresenter.setReordView(new IOrderRecordView() {
                @Override
                public void showOrderRecordList(List<OrderHistoryType> list) {
                    OrderRecordActivity.this.showOrderRecordList(list);
                }
                @Override
                public void showEmpty() {
                    OrderRecordActivity.this.showEmpty();
                }
                @Override
                public void showErr(Throwable throwable) {
                    OrderRecordActivity.this.showErr(throwable);
                }
                @Override
                public void showMoreLayout(boolean isShow) {
                    OrderRecordActivity.this.showMoreLayout(isShow);
                }
            });
        }
        return recordsPresenter;
    }

    private void showOrderRecordList(List<OrderHistoryType> list) {
        recordsAdapter.addOrders(list);
        cndl_noDatas.setVisibility(View.GONE);
        progressLayout.setVisibility(View.GONE);
        rl_list.setVisibility(View.VISIBLE);
    }

    private void showEmpty() {
        cndl_noDatas.setVisibility(View.VISIBLE);
        progressLayout.setVisibility(View.GONE);
        rl_list.setVisibility(View.GONE);

        cndl_noDatas.setHintText(R.string.no_order_data);
        cndl_noDatas.setRefreshImage(R.drawable.no_order_data);
        cndl_noDatas.showIcon(true);
        cndl_noDatas.setOnRefreshListener(null);
    }

    private void showErr(Throwable throwable) {
        pageNum --;
        pageSize = 20;
        if (recordsAdapter.getCount() <= 0) {
            cndl_noDatas.setVisibility(View.VISIBLE);
            progressLayout.setVisibility(View.GONE);
            rl_list.setVisibility(View.GONE);

            cndl_noDatas.setHintText(R.string.no_ordre_list_data_error);
            cndl_noDatas.setRefreshImage(R.drawable.common_refresh_data);
            cndl_noDatas.showIcon(true);
            cndl_noDatas.setOnRefreshListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    loadOrderRecords();
                }
            });
        }else{
            more_progress.setVisibility(View.GONE);
            tv_more.setVisibility(View.VISIBLE);
        }
    }

    private void showMoreLayout(boolean isShow) {
        more_progress.setVisibility(View.GONE);
        tv_more.setVisibility(View.VISIBLE);
        rl_more.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (recordsPresenter != null) {
            recordsPresenter.finish();
            recordsPresenter = null;
        }
    }
}
